//
//  UIButtonCategory.m
//  ubixar-utils
//
//  Created by Demis Bellot on 17/04/2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "UIButtonCategory.h"
#import "UXLog.h"

@implementation UIButton(UIButtonCategory) 

- (void) addClickHandler:(SEL)onClickSel forTarget:(id)target 
{
//	UXLog(UXLoggerUI, @"registered click handler: '%@', on target: '%@'", 
//		  NSStringFromSelector(onClickSel), target);
	[self addTarget:target action:onClickSel forControlEvents:UIControlEventTouchUpInside];
}

//Usage [button setImagesUsingImageNameFormat:@"playButton-%@.png"]; 
- (void) setImagesUsingImageNameFormat: (NSString*)imageNameFormat 
{
	DLog(@"getting here...");
	[self setImagesUsingImageNameFormat:imageNameFormat useDefaultForMissingImages:TRUE];
}

- (void) setImagesUsingImageNameFormat: (NSString*)imageNameFormat useDefaultForMissingImages:(BOOL)useDefault
{
	NSString* defaultImageName = [NSString stringWithFormat:imageNameFormat, @"normal"];
	NSString* selectedImageName = [NSString stringWithFormat:imageNameFormat, @"selected"];
	NSString* highlightedImageName = [NSString stringWithFormat:imageNameFormat, @"highlighted"];
	NSString* disabledImageName = [NSString stringWithFormat:imageNameFormat, @"disabled"];
	
	UIImage* defaultImage = [UIImage imageNamed:defaultImageName];
	//Only use the default image if it exists
	useDefault = useDefault && defaultImage != NULL;
	if (defaultImage != NULL) [self setImage:defaultImage forState:UIControlStateNormal];
	
	UIImage* selectedImage = [UIImage imageNamed:selectedImageName];
	if (selectedImage == NULL && useDefault) selectedImage = defaultImage;
	if (selectedImage != NULL) [self setImage:selectedImage	forState:UIControlStateSelected];
	
	UIImage* highlightedImage = [UIImage imageNamed:highlightedImageName];
	if (highlightedImage == NULL && useDefault) highlightedImage = defaultImage;
	if (highlightedImage != NULL) [self setImage:highlightedImage	forState:UIControlStateHighlighted];
	
	UIImage* disabledImage = [UIImage imageNamed:disabledImageName];
	if (disabledImage == NULL && useDefault) disabledImage = defaultImage;
	if (disabledImage != NULL) [self setImage:disabledImage	forState:UIControlStateDisabled];	
}

- (void) setImagesUsingDefault: (NSString*)defaultImageName 
{
	[self setImagesUsingDefault: defaultImageName 
					   selected: defaultImageName
					highlighted: defaultImageName
					   disabled: defaultImageName];
}

- (void) setImagesUsingDefault: (NSString*)defaultImageName 
					       selected: (NSString*)selectedImageName
{
	[self setImagesUsingDefault: defaultImageName 
					   selected: selectedImageName
					highlighted: defaultImageName
					   disabled: defaultImageName];
}

- (void) setImagesUsingDefault: (NSString*)defaultImageName 
					       selected: (NSString*)selectedImageName
						highlighted: (NSString*)highlightedImageName
{
	[self setImagesUsingDefault: defaultImageName 
					   selected: selectedImageName
					highlighted: highlightedImageName
					   disabled: defaultImageName];
}

- (void) setImagesUsingDefault: (NSString*)defaultImageName 
						   selected: (NSString*)selectedImageName
						highlighted: (NSString*)highlightedImageName
						   disabled: (NSString*)disabledImageName
{
	[self setImage:[UIImage imageNamed:defaultImageName]		forState:UIControlStateNormal];
	[self setImage:[UIImage imageNamed:selectedImageName]		forState:UIControlStateSelected];
	[self setImage:[UIImage imageNamed:highlightedImageName]	forState:UIControlStateHighlighted];
	[self setImage:[UIImage imageNamed:disabledImageName]		forState:UIControlStateDisabled];
}

@end
